package controller

import (
	"admin/model"
	"common/helper"
	"common/util"
	"encoding/json"
	"errors"
	g "github.com/doug-martin/goqu/v9"
	"github.com/valyala/fasthttp"
	"strconv"
)

const (
	CfgValidInviteWagedReq   = 234 //有效邀请人数条件:最低打码
	CfgValidInviteMinDeposit = 114 //有效邀请人数最低充值额度
	ProxyOneLevel            = 235 //代理1级流水返还比例(百分比)
	ProxyTowLevel            = 236 //代理2级流水返还比例(百分比)
	ProxyThreeLevel          = 237 //代理3级流水返还比例(百分比)
)

type ConfigValue struct {
	Waged      string `json:"waged"`
	MinDeposit string `json:"minDeposit"`
}

type ProxyLevel struct {
	Level      uint32 `json:"level"`
	Proportion string `json:"proportion"`
}

type ConfigProxyInviteBonusController struct {
	util.ControllerImpl
}

var ConfigProxyInviteBonusModel = &model.ConfigProxyInviteBonusModel{
	TableName: "tbl_cfg_proxy_invite_bonus",
}

func (that *ConfigProxyInviteBonusController) GetModel() *model.ConfigProxyInviteBonusModel {
	return ConfigProxyInviteBonusModel
}

func (that *ConfigProxyInviteBonusController) GetProxyConfig(ctx *fasthttp.RequestCtx) {
	values := []int32{
		ProxyOneLevel, ProxyTowLevel, ProxyThreeLevel,
	}
	record := model.GetProxyLevelGameConfig(values)
	helper.RetData(ctx, true, record)
}

func (that *ConfigProxyInviteBonusController) UpdateProxyConfig(ctx *fasthttp.RequestCtx) {
	that.Execution(ctx, func(params []byte) error {
		ent := model.TblGameConfig{}
		if e := json.Unmarshal(params, &ent); e != nil {
			return errors.New("参数错误")
		}
		numberCode, err := strconv.Atoi(ent.CfgType)
		if err != nil {
			return err
		}
		if err = model.GameConfigUpdateValueByType(numberCode, ent.CfgValue); err != nil {
			return err
		}
		helper.RetData(ctx, true, "成功")
		return nil
	})
}

func (that *ConfigProxyInviteBonusController) GetConfig(ctx *fasthttp.RequestCtx) {
	cv := ConfigValue{
		Waged:      model.GameConfigByType(CfgValidInviteWagedReq).CfgValue,
		MinDeposit: model.GameConfigByType(CfgValidInviteMinDeposit).CfgValue,
	}
	helper.RetData(ctx, true, cv)
}

func (that *ConfigProxyInviteBonusController) UpdateConfig(ctx *fasthttp.RequestCtx) {
	that.Execution(ctx, func(params []byte) error {
		ent := ConfigValue{}
		if e := json.Unmarshal(params, &ent); e != nil {
			return errors.New("参数错误")
		}
		err := model.GameConfigUpdateValueByType(CfgValidInviteWagedReq, ent.Waged)
		if err != nil {
			return err
		}
		err = model.GameConfigUpdateValueByType(CfgValidInviteMinDeposit, ent.MinDeposit)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, "成功")
		return nil
	})
}

func (that *ConfigProxyInviteBonusController) Insert(ctx *fasthttp.RequestCtx) {
	that.Execution(ctx, func(params []byte) error {
		ent := model.ConfigProxyInviteBonusParams{}
		if e := json.Unmarshal(params, &ent); e != nil {
			return errors.New("参数错误")
		}
		err := that.GetModel().Insert(ent)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, "成功")
		return nil
	})
}
func (that *ConfigProxyInviteBonusController) Update(ctx *fasthttp.RequestCtx) {
	that.Execution(ctx, func(params []byte) error {
		ent := model.ConfigProxyInviteBonus{}
		if e := json.Unmarshal(params, &ent); e != nil {
			return errors.New("参数错误")
		}
		err := that.GetModel().Update(ent)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, "成功")
		return nil
	})
}
func (that *ConfigProxyInviteBonusController) Delete(ctx *fasthttp.RequestCtx) {
	that.ExecutionGetId(ctx, func(id uint32) error {
		err := that.GetModel().DeleteById(id)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, "成功")
		return nil
	})
}
func (that *ConfigProxyInviteBonusController) Get(ctx *fasthttp.RequestCtx) {
	that.ExecutionGetId(ctx, func(id uint32) error {
		ent := model.ConfigProxyInviteBonus{}
		err := that.GetModel().Get(id, &ent)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, ent)
		return nil
	})
}
func (that *ConfigProxyInviteBonusController) GetByEx(ctx *fasthttp.RequestCtx, ex g.Expression) {
	ent := model.ConfigProxyInviteBonus{}
	err := that.GetModel().GetByEx(ex, &ent)
	if err == nil {
		helper.RetData(ctx, true, ent)
		return
	}
	helper.RetData(ctx, false, err)
}
func (that *ConfigProxyInviteBonusController) Page(ctx *fasthttp.RequestCtx) {
	that.ExecutionPage(ctx, func(page *model.ConfigProxyInviteBonusPageParams) error {
		err := that.GetModel().Page(page)
		if err != nil {
			return err
		}
		helper.RetData(ctx, true, page)
		return nil
	})
}
func (that *ConfigProxyInviteBonusController) ExecutionPage(ctx *fasthttp.RequestCtx, f func(page *model.ConfigProxyInviteBonusPageParams) error) {
	that.Execution(ctx, func(params []byte) error {
		pageParams := &model.ConfigProxyInviteBonusPageParams{}
		if e := json.Unmarshal(params, &pageParams); e != nil {
			return errors.New("参数错误")
		}
		return f(pageParams)
	})
}
